package com.controller;

import com.pojo.Strangecar;
import com.service.*;
import com.vo.Result;
import com.vo.StopcarmoneyVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Controller
@RequestMapping("/temporaryCar")
public class temporaryCarController {
    @Resource
    private StrangecarService strangecarService;
    @Resource
    private ParkruleService parkRuleService;
    @Resource
    private ParkingLotService parkingLotService;
    @Resource
    private StopcarmoneyService stopCarMoneyService;
    @Resource
    private TrafficrecordsService trafficreCordsService;
    @Resource
    private ParkService parkService;
    @RequestMapping("/totemporaryCar")
    public String toTemporparyCar(){
        return "/CommunityParking/temporaryCar";
    }
    @ResponseBody
    @RequestMapping("/addStrangeCar")
    public Result addStrangeCar(Strangecar strangecar){
        try {
            int i = strangecarService.addStrangeCar(strangecar);
            return Result.success("开始计时");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("失败");
        }
    }
    @ResponseBody
    @RequestMapping("/selectAllStrangeCar")
    public Result selectAllStrangeCar(){
        List<Strangecar> strangecars = strangecarService.selectAllStrangeCar();
        return Result.success(strangecars);
    }
    @ResponseBody
    @RequestMapping("/updateParklotState")
    public Result updateParklotState(Integer id) {
        try {
            int i = parkingLotService.updateNullStateById(id);
            return Result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("失败");
        }
    }
    @ResponseBody
    @RequestMapping("/deleteStrangeCarById")
    public Result deleteStrangeCarById(Integer id) {
        try {
            int i = strangecarService.deleteStrangeCarById(id);
            return Result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("失败");
        }
    }
    @ResponseBody
    @RequestMapping("/addTrafficrecords")
    public Result addTrafficrecords(@RequestBody StopcarmoneyVo stopcarmoneyVo) {
        System.out.println(stopcarmoneyVo);
        LocalDateTime localDateTime = LocalDateTime.now();
        String endtime = localDateTime.toString();
        stopcarmoneyVo.setEndtime(endtime);
        stopcarmoneyVo.setOwnername(stopcarmoneyVo.getName());
        stopcarmoneyVo.setOwnerphone(stopcarmoneyVo.getPhone());
        System.out.println(stopcarmoneyVo);
/*
        Parkinglot parkinglot = parkingLotService.selectParkingLotById(stopcarmoneyVo.getParkinglotid());
*/
        stopcarmoneyVo.setParklotname(stopcarmoneyVo.getParkinglot().getParkingcode());
/*
        Park park = parkService.selectParkById(stopcarmoneyVo.getParkid());
*/
        stopcarmoneyVo.setParkname(stopcarmoneyVo.getPark().getParkcode());
        try {
            int i = trafficreCordsService.addTrafficreCords(stopcarmoneyVo);
            return Result.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("失败");
        }
    }
    @ResponseBody
    @RequestMapping("/addStopCarMoney")
    public Result addStopCarMoney(@RequestBody StopcarmoneyVo stopcarmoneyVo) {
        LocalDateTime localDateTime = LocalDateTime.now();
        String endtime = localDateTime.toString();
        stopcarmoneyVo.setEndtime(endtime);
        stopcarmoneyVo.setOwnername(stopcarmoneyVo.getName());
        stopcarmoneyVo.setOwnerphone(stopcarmoneyVo.getPhone());
/*
        Parkinglot parkinglot = parkingLotService.selectParkingLotById(stopcarmoneyVo.getParkinglotid());
*/
        stopcarmoneyVo.setParklotname(stopcarmoneyVo.getParkinglot().getParkingcode());
/*
        Park park = parkService.selectParkById(stopcarmoneyVo.getParkid());
*/

        stopcarmoneyVo.setParkname(stopcarmoneyVo.getPark().getParkcode());
        stopcarmoneyVo.setIsno(1);
/*
        Parkrule parkrule = parkRuleService.selectParkRuleById(stopcarmoneyVo.getRuleid());
*/
        stopcarmoneyVo.setMoney(stopcarmoneyVo.getParkrule().getMoney());
        String time=stopcarmoneyVo.getStarttime();
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime starttime=LocalDateTime.parse(time,dateTimeFormatter);

        // 计算差异
        Duration duration = Duration.between(starttime,localDateTime);
        long freetime=stopcarmoneyVo.getParkrule().getFreetime();
        long minutes = duration.toMinutes(); // 相差的小时数
        long m=0;
        long mm=0;
        long zong=0;
        if(minutes<freetime){
            stopcarmoneyVo.setIsno(1);
            stopcarmoneyVo.setZomoney(0);
            int i = stopCarMoneyService.addStopCarMoney(stopcarmoneyVo);
            return Result.success("已离开");
        }else if(minutes>=freetime){
            //一天总钱数/小时数=收费小时数
            int moneyhours=stopcarmoneyVo.getParkrule().getDaymoney()/stopcarmoneyVo.getParkrule().getMoney();
            //停车分钟数/60=计算总天数；分两种情况 1、总天数小于收费小时数 2、大于一天
            double totalhours=minutes/60;//总小时数
            long totalday=minutes/60/24;//总天数
            long other=minutes-totalday*24*60;//除去整天之外的剩余分钟数
            long otherhours=other/60;//剩余的小时
            double otherhoursmoney=otherhours*stopcarmoneyVo.getParkrule().getMoney();//剩余的钱数
            double totaldaymoney=totalday*stopcarmoneyVo.getParkrule().getDaymoney();//总天数的前
            double zonghoursdaymoney=otherhoursmoney+totaldaymoney;//总钱数
            stopcarmoneyVo.setZomoney((int) zonghoursdaymoney);

            int i = stopCarMoneyService.addStopCarMoney(stopcarmoneyVo);
        }
        return Result.success("已离开");
    }

}
